//
//  ZH_HotRecommendedableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/19.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_HotRecommendedableViewCell.h"
#import "ZH_HotRecommendeCollectionViewCell.h"
@interface ZH_HotRecommendedableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)UILabel *titleLabel;
@property (nonatomic,weak)UIImageView *leftImage;
@property (nonatomic,weak)UIImageView *rightImage;
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ZH_HotRecommendedableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_HotRecommendedableViewCell";
    ZH_HotRecommendedableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_HotRecommendedableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
}
- (void)loadSubviewConstraints{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(FITSCALE(12));
        make.right.equalTo(self.contentView).offset(FITSCALE(-12));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(FITSCALE(16));
        make.centerX.equalTo(self.contentView);
    }];
    [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.titleLabel.mas_left).offset(FITSCALE(-20));
    }];
    [self.rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.left.equalTo(self.titleLabel.mas_right).offset(FITSCALE(20));
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(FITSCALE(15));
        make.left.equalTo(self.bgView).offset(FITSCALE(10));
        make.right.equalTo(self.bgView).offset(FITSCALE(-10));
        make.bottom.equalTo(self.bgView).offset(FITSCALE(15));
    }];
}
///更新 ui
- (void)updateUiForData:(NSMutableArray *)datas
{
    if(datas.count > 0){
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:datas];
        
        NSInteger item = self.dataArray.count;
        NSInteger rowNum = (item / 2) + ((item % 2) > 0 ? 1 : 0);
        
        CGFloat collectionHeight = FITSCALE(214) * rowNum + (rowNum * FITSCALE(8));
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(collectionHeight);
        }];
        
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(HEIGHT_TRANSFORMATION(0));
        }];
    }
}


#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
  
    
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake((kMainScreenWidth - FITSCALE(52)) / 2, FITSCALE(214));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return FITHEIGHT(8);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZH_HotRecommendeCollectionViewCell *cell = [ZH_HotRecommendeCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    [cell updataUI:self.dataArray[indexPath.row]];
    return cell;
}



#pragma mark -- getter,setter

///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionVertical];
        //        collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [ZH_HotRecommendeCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}


///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}
#pragma mark - getter,setter
- (UIView *)bgView{
    if (!_bgView) {
        UIView *view = [UIView createViewWithBackgroundColor:kCommonWhiteBg];
        view.layer.cornerRadius = FITSCALE(3);
        view.layer.masksToBounds = YES;
        view.clipsToBounds = YES;
        [self.contentView addSubview:_bgView = view];
    }
    return _bgView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [UILabel createLabelWithText:@"推荐商品" font:kFontSize_30];
        label.font = [UIFont boldSystemFontOfSize:15];
        [self.contentView addSubview:_titleLabel = label];
    }
    return _titleLabel;
}
- (UIImageView *)leftImage{
    if (!_leftImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"img_tip_left"];
        [self.contentView addSubview:_leftImage = image];
    }
    return _leftImage;
}
- (UIImageView *)rightImage{
    if (!_rightImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"img_tip_right"];
        [self.contentView addSubview:_rightImage = image];
    }
    return _rightImage;
}
@end
